package com.mvp.lt.airlineview.map;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.view.LayoutInflater;


import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polygon;
import com.amap.api.maps.model.Polyline;
import com.luck.picture.lib.tools.DateUtils;
import com.mvp.lt.airlineview.R;
import com.mvp.lt.airlineview.base.MapCalculationUtil;
import com.mvp.lt.airlineview.base.OnDataChangeListener;
import com.mvp.lt.airlineview.databinding.DroneMiddlePointMarkerBinding;
import com.mvp.lt.airlineview.databinding.DroneRegionPointMarkerBinding;
import com.mvp.lt.airlineview.location.DipType;
import com.mvp.lt.airlineview.location.MapMarkerTypeConstant;
import com.mvp.lt.airlineview.utils.AndroidUtil;
import com.mvp.lt.airlineview.utils.LogUtil;
import com.mvp.lt.airlineview.utils.RouteUtil;
import com.mvp.lt.airlineview.utils.UIUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author LiuTao
 * @description: 倾斜摄影航线规划
 * @date : 2020/12/28 14:02
 */
public class ObliquePhotographyUtil {
    private static ObliquePhotographyUtil sAerialPhotographyUtil;
    /**
     * 航线
     */
    private Polyline mRoutePolyline;

    /**
     * 原始数据
     */
    private List<LatLng> mOrangeLatLngs = new ArrayList<>();
    /**
     * 航线点
     */
    private List<LatLng> mRouteLatLngs = new ArrayList<>();
    private List<LatLng> mLeftRouteLatLngs = new ArrayList<>();
    private List<LatLng> mTopRouteLatLngs = new ArrayList<>();
    private List<LatLng> mRightRouteLatLngs = new ArrayList<>();
    private List<LatLng> mBottomRouteLatLngs = new ArrayList<>();
    /**
     * 区域面
     */
    private Polygon mDrawPolygonOptions;
    /**
     * 区域线
     */
    private List<Polyline> mDrawPolylines = new ArrayList<>();
    /**
     * 区域点
     */
    private List<Marker> mDrawMarkers = new ArrayList<>();
    /**
     * 带加号的区域点
     */
    private List<Marker> mDrawPlusMarkers = new ArrayList<>();
    /**
     * 区域点经纬度
     */
    private List<LatLng> mDrawLatLngs = new ArrayList<>();
    /**
     * 带加号的区域点
     */
    private List<LatLng> mDrawPlusLatLng = new ArrayList<>();

    /**
     * 阴影
     */
    private List<LatLng> mBoundShadowDrawLatLngs = new ArrayList<>();
    /**
     * 主航线角度
     * 机头旋转方向度[0-360]
     */
    private int mHeadDirection = 270;

    private int centerHeadValue = 0;
    private int topHeadValue = 0;
    private int rightHeadValue = 0;
    private int leftHeadValue = 0;
    private int bottomHeadValue = 0;
    /**
     * 边距 0 -100 m
     */
    private int edgeDistance = 0;
    /**
     * 盘向重叠率 80%  [10,90]
     */
    private int diskOverlapRatio = 80;
    /**
     * 航线间隔 m
     */
    private int routeIntervalDistance = 20;
    /**
     * 航向重叠率 计算拍照间隔[10,90]
     */
    private int courseOverlapRatio = 80;
    /**
     * 航向 拍照距离间距 m
     * 可以用于计算拍照时间间隔
     */
    private int courseIntervalDistance = 20;
    /**
     * 航向间隔时间（秒）
     */
    private double courseIntervalTime = 3;
    /**
     * 飞行高度 m
     */
    private int flyHeight = 50;
    /**
     * GSD
     */
    private double baseGsd = 2.74;
    private double gsd = 2.74;

    /**
     * 飞行速度 m/s
     */
    private int flySpeed = 5;
    /**
     * 倾斜角度[-85 ,-45]°
     * 用于设置云台俯仰角
     */
    private int pitchAngle = 85;
    /**
     * 等效焦距 单位毫米
     */
    private double focalLength = 24;
    /**
     * 长画幅 单位毫米
     */
    private int longFormat = 35;
    /**
     * 短画幅 单位毫米
     */
    private int shortFormat = 24;
    /**
     * 类型
     * 1:中心  2左   3上   4右  5下
     */
    private int dipType = DipType.TYPE_CENTER;
    /**
     * 边线是否相交
     */
    private boolean isIntersectDrag = false;

    /**
     * 当前选中的marker
     */
    private Marker mCurrentEdgMarker;
    /**
     * 起始点
     */
    private Marker mStartMarker;
    /**
     * 判断交点
     */
    private LatLng mLatLng1;
    private LatLng mLatLng2;
    private LatLng mDragLatLng;
    /**
     * 拖动之前的区域点  用来复原
     */
    private LatLng dragStartLatLng;
    /**
     * 当前点
     */
    private int clickPosition;

    public OnDataChangeListener mOnDataChangeListener;
    private PhotographBean mPhotographBean;
    private DecimalFormat mDecimalFormat;


    /**
     * 数据改变
     *
     * @param onDataChangeListener
     */
    public void setOnDataChangeListener(OnDataChangeListener onDataChangeListener) {
        mOnDataChangeListener = onDataChangeListener;
    }

    public static ObliquePhotographyUtil getInstance() {
        if (sAerialPhotographyUtil == null) {
            sAerialPhotographyUtil = new ObliquePhotographyUtil();
        }
        return sAerialPhotographyUtil;
    }


    public void onDestroy() {
        sAerialPhotographyUtil = null;
    }


    public void setmHeadDirection(int mHeadDirection) {
        this.mHeadDirection = mHeadDirection;
        changeFiveDirectionValue();
        setHeadDirectionDraw(dipType);
    }

    public void setPhotographBean(PhotographBean photographBean) {
        this.mPhotographBean = photographBean;
        if (mOnDataChangeListener != null) {
            mOnDataChangeListener.dataChange();
        }
    }

    public void setDrawLatLngs(List<LatLng> drawLatLngs) {
        mDrawLatLngs = drawLatLngs;
    }

    public List<LatLng> getDrawLatLngs() {
        return mDrawLatLngs;
    }

    /**
     * 初始化数据
     */
    public void initData(PhotographBean photographBean) {
        this.mPhotographBean = photographBean;
        mDrawLatLngs = new ArrayList<>();
        mHeadDirection = photographBean.getMainCourseAngle();
        diskOverlapRatio = photographBean.getDiskOverlapRatio();
        courseOverlapRatio = photographBean.getCourseOverlapRatio();
        flyHeight = photographBean.getFlyHeight();
        flySpeed = photographBean.getRouteSpeed();
        baseGsd = photographBean.getBaseGsd();
        focalLength = photographBean.getFocalLength();
        edgeDistance = photographBean.getMargin();
        pitchAngle = 90 - photographBean.getTiltAngle();
        setRouteCourseIntervalDistance(flyHeight, focalLength, shortFormat, courseOverlapRatio);
        setRouteIntervalDistance(flyHeight, focalLength, longFormat, diskOverlapRatio);
        setGsd(flyHeight);
        changeFiveDirectionValue();
    }

    /**
     * 数据回显
     *
     * @param context
     * @param latLngs
     */
    public void loadData(Context context, List<LatLng> latLngs) {
        this.mDrawLatLngs = latLngs;
        drawEdgLineMarker(context);
    }


    /**
     * 画多边形区域
     *
     * @param context
     * @param latLng
     */
    public void onAddDraw(Context context, LatLng latLng) {
        if (mDrawLatLngs.size() > 0) {
            return;
        }
        //mDrawLatLngs = new ArrayList<>();

        LatLng latLng1Edge = MapCalculationLogLatDistance.getBearingLatLng(latLng, 370, 45);
        LatLng latLng2Edge = MapCalculationLogLatDistance.getBearingLatLng(latLng, 370, 135);
        LatLng latLng3Edge = MapCalculationLogLatDistance.getBearingLatLng(latLng, 370, 225);
        LatLng latLng5Edge = MapCalculationLogLatDistance.getBearingLatLng(latLng, 370, 315);
        mDrawLatLngs.add(latLng1Edge);
        mDrawLatLngs.add(latLng2Edge);
        mDrawLatLngs.add(latLng3Edge);
        mDrawLatLngs.add(latLng5Edge);
        MapCreateManager.getMapManager().autoAnimateCameraMap(mDrawLatLngs);
        drawEdgLineMarker(context);
    }

    /**
     * 画区域线和点
     *
     * @param context
     */
    private void drawEdgLineMarker(Context context) {
        //画点
        for (int i = 0; i < mDrawLatLngs.size(); i++) {
            LatLng mLatLng = mDrawLatLngs.get(i);
            mCurrentEdgMarker = ObliquePhotographyUtil.getInstance().addMarker(context, mLatLng, i + 1, true);
            mDrawMarkers.add(mCurrentEdgMarker);
            if (i == mDrawLatLngs.size() - 1) {
                LatLng mNextLatLng = mDrawLatLngs.get(0);
                Marker plusMarker = ObliquePhotographyUtil.getInstance().addMiddleMarker(context, mLatLng, mNextLatLng, i + 1, true);
                mDrawPlusMarkers.add(plusMarker);
                mDrawPlusLatLng.add(plusMarker.getPosition());
                Polyline polyline = MapCreateManager.getMapManager().drawTwoPointBlueLine(mLatLng, mNextLatLng);
                mDrawPolylines.add(polyline);
            } else {
                LatLng mNextLatLng = mDrawLatLngs.get(i + 1);
                Marker plusMarker = ObliquePhotographyUtil.getInstance().addMiddleMarker(context, mLatLng, mNextLatLng, i + 1, true);
                mDrawPlusMarkers.add(plusMarker);
                mDrawPlusLatLng.add(plusMarker.getPosition());
                Polyline polyline = MapCreateManager.getMapManager().drawTwoPointBlueLine(mLatLng, mNextLatLng);
                mDrawPolylines.add(polyline);
            }
        }
        setHeadDirectionDraw(dipType);
    }

    /**
     * 正射
     */
    private void updateDraw(int headDirection) {
        if (mDrawLatLngs == null || mDrawLatLngs.size() == 0) {
            return;
        }
        //外接矩形 =>/旋转后的区域点
        //1
        List<LatLng> mBoundsLatLng = RouteUtil.createPolygonBounds(mDrawLatLngs);
        //2
        List<LatLng> rPolygons = RouteUtil.createRotatePolygon(mDrawLatLngs, mBoundsLatLng, -headDirection);
        //3
        List<LatLng> mRotateBoundingBox = RouteUtil.createPolygonBounds(rPolygons);
        //todo
        double scale = getScale();
        mBoundShadowDrawLatLngs = RouteUtil.createRotatePolygon(rPolygons, mBoundsLatLng, headDirection, scale, scale);
        if (mDrawPolygonOptions == null) {
            mDrawPolygonOptions = MapCreateManager.getMapManager().drawPolygonOptions(mBoundShadowDrawLatLngs, AndroidUtil.getColor(R.color.transparent), AndroidUtil.getColor(R.color.dodgerblue50));
        } else {
            mDrawPolygonOptions.setPoints(mBoundShadowDrawLatLngs);
        }
        //穿过的纬度 0：线条数量 ，1：相差纬度
        Double[] latLines = RouteUtil.createLats(mRotateBoundingBox, routeIntervalDistance);
        //条数
        int numberRoutes = (int) Math.ceil(latLines[0]);
        List<LatLng> polyLines = new ArrayList<>();
        List<LatLng> boundsList = new ArrayList<>();
        //遍历航线条数
        for (int i = 0; i < numberRoutes + 1; i++) {
            List<LatLng> lines = new ArrayList<>();
//            double fen = (latLines[1]) / 3 * 2;
            //遍历 西北方向的点
            LatLng westNorthLatLng = mRotateBoundingBox.get(1);
            double lat = westNorthLatLng.latitude - i * latLines[1];
            for (int j = 0; j < rPolygons.size(); j++) {
                int j2;
                if (j == rPolygons.size() - 1) {
                    j2 = 0;
                } else {
                    j2 = j + 1;
                }
                LatLng latLng1 = rPolygons.get(j);
                LatLng latLng2 = rPolygons.get(j2);
                LatLng checkLatLng = RouteUtil.getCrossPoint(latLng1, latLng2, lat);
                if (checkLatLng != null) {
                    lines.add(checkLatLng);
                }
            }
            if (lines.size() < 2) {
                continue;
            }
            if (lines.get(0).longitude == lines.get(1).longitude) {
                continue;
            }
            //排序 从小到大
            /*
             * int compare(Student o1, Student o2) 返回一个基本类型的整型，
             * 返回负数表示：o1 小于o2，
             * 返回0 表示：o1和o2相等，
             * 返回正数表示：o1大于o2。
             */
            Collections.sort(lines, new Comparator<LatLng>() {
                @Override
                public int compare(LatLng o1, LatLng o2) {
                    if (o1.longitude > o2.longitude) {
                        return -1;
                    }
                    if (o1.longitude < o2.longitude) {
                        return 1;
                    }
                    return 0;
                }
            });
            boundsList.add(lines.get(0));
            boundsList.add(boundsList.size(), lines.get(lines.size() - 1));
            if (i % 2 == 0) {
                double lngUnits = getLngUnits(lines.get(0).latitude);
                for (int j = lines.size() - 1; j >= 0; j--) {
                    LatLng latLng = lines.get(j);
                    if (j == 0) {
                        LatLng latLng1 = new LatLng(latLng.latitude, latLng.longitude + lngUnits);
                        polyLines.add(latLng1);
                    } else if (j == lines.size() - 1) {
                        LatLng latLng1 = new LatLng(latLng.latitude, latLng.longitude - lngUnits);
                        polyLines.add(latLng1);
                    } else {
                        polyLines.add(latLng);

                    }
                }
            } else {
                double lngUnits = getLngUnits(lines.get(0).latitude);
                for (int j = 0; j < lines.size(); j++) {
                    LatLng latLng = lines.get(j);
                    if (j == 0) {
                        LatLng latLng1 = new LatLng(latLng.latitude, latLng.longitude + lngUnits);
                        polyLines.add(latLng1);
                    } else if (j == lines.size() - 1) {
                        LatLng latLng1 = new LatLng(latLng.latitude, latLng.longitude - lngUnits);
                        polyLines.add(latLng1);
                    } else {
                        polyLines.add(latLng);
                    }
                }
            }
        }
        if (polyLines.size() > 0) {
            mOrangeLatLngs = RouteUtil.createRotatePolygon(polyLines, mBoundsLatLng, headDirection);
            updateWayLineByType(mOrangeLatLngs);
        }
        postAllData();
    }


    /**
     * 根据一个点的经纬度 ，方位角和距离得到另外一个点的经纬度
     * 倾斜之后的经纬度
     *
     * @param tiltDistance  m
     * @param latLng
     * @param pitchAngle：角度
     * @return
     */
    public LatLng getTiltLatlng(LatLng latLng, double tiltDistance, double pitchAngle) {
        return new LatLng(latLng.latitude + (tiltDistance * Math.cos(pitchAngle * Math.PI / 180)) / 111000.0,
                latLng.longitude + (tiltDistance * Math.sin(pitchAngle * Math.PI / 180))
                        / (111000.0 * Math.cos(latLng.latitude * Math.PI / 180))
        );
    }

    public void setTiltAngle(int tiltAngle) {
        LogUtil.e("数据", "=======角度========>" + tiltAngle);
        this.pitchAngle = Math.abs(tiltAngle);
        updateWayLineByType(mOrangeLatLngs);
    }

    /**
     * 倾斜距离
     */
    public double getTiltDistance() {
        return flyHeight * Math.tan(Math.toRadians(Math.abs(pitchAngle)));
    }


    /**
     * 设置倾斜方向 五方向
     *
     * @param dipType
     */
    public void setDipType(@DipType int dipType) {
        this.dipType = dipType;
        setHeadDirectionDraw(dipType);
    }

    /**
     * 获取当前方向 五方向
     *
     * @return
     */
    public int getDipType() {
        return dipType;
    }

    /**
     * 倾斜方向
     *
     * @param dipType
     */
    private void setHeadDirectionDraw(@DipType int dipType) {
        if (dipType == DipType.TYPE_CENTER) { //中
            setHeadCenterDirectionDraw();
        }
        if (dipType == DipType.TYPE_BOTTOM) {
            setHeadBottomDirectionDraw();
        }
        if (dipType == DipType.TYPE_RIGHT) {
            setHeadRightDirectionDraw();
        }
        if (dipType == DipType.TYPE_TOP) {
            setHeadTopDirectionDraw();
        }
        if (dipType == DipType.TYPE_LEFT) {
            setHeadLeftDirectionDraw();
        }
    }

    private void setHeadCenterDirectionDraw() {
        updateDraw(mHeadDirection);
    }

    private void setHeadTopDirectionDraw() {
        updateDraw(mHeadDirection);
    }

    private void setHeadBottomDirectionDraw() {
        updateDraw(mHeadDirection + 180);
    }

    private void setHeadLeftDirectionDraw() {
        updateDraw(mHeadDirection - 90);

    }

    private void setHeadRightDirectionDraw() {
        updateDraw(mHeadDirection + 90);
    }

    /**
     * 获取五方位角度
     */
    public void changeFiveDirectionValue() {
        if (dipType == DipType.TYPE_CENTER) {
            centerHeadValue = MapCreateManager.getMapManager().parseCenterOrTopRotationToHead(mHeadDirection);
            LogUtil.e("正射", "TYPE_CENTER=====机头方向>" + centerHeadValue);
        }
        if (dipType == DipType.TYPE_RIGHT) {
            rightHeadValue = MapCreateManager.getMapManager().parseRightRotationToHead(mHeadDirection + 90);
            LogUtil.e("正射", "TYPE_RIGHT=====机头方向>" + rightHeadValue);
        }
        if (dipType == DipType.TYPE_BOTTOM) {
            bottomHeadValue = MapCreateManager.getMapManager().parseBottomRotationToHead(mHeadDirection + 180);
            LogUtil.e("正射", "TYPE_BOTTOM=====机头方向>" + bottomHeadValue);
        }
        if (dipType == DipType.TYPE_LEFT) {//180 --->90
            leftHeadValue = MapCreateManager.getMapManager().parseLeftRotationToHead(mHeadDirection - 90);
            LogUtil.e("正射", "TYPE_LEFT=====机头方向>" + leftHeadValue);
        }
        if (dipType == DipType.TYPE_TOP) {
            topHeadValue = MapCreateManager.getMapManager().parseCenterOrTopRotationToHead(mHeadDirection);
            LogUtil.e("正射", "TYPE_TOP=====机头方向>" + topHeadValue);
        }

    }

    public int getHeadDirection() {
        return mHeadDirection;
    }


    public int getCenterHeadValue() {
        return centerHeadValue;
    }

    public int getTopHeadValue() {
        return topHeadValue;
    }

    public int getRightHeadValue() {
        return rightHeadValue;
    }

    public int getLeftHeadValue() {
        return leftHeadValue;
    }

    public int getBottomHeadValue() {
        return bottomHeadValue;
    }


    /**
     * 获取固定机头方向
     *
     * @return
     */
    public int getHeaValue() {
        if (dipType == DipType.TYPE_CENTER) {
            return centerHeadValue;
        }
        if (dipType == DipType.TYPE_RIGHT) {
            return rightHeadValue;
        }
        if (dipType == DipType.TYPE_BOTTOM) {
            return bottomHeadValue;
        }
        if (dipType == DipType.TYPE_LEFT) {//180 --->90
            return leftHeadValue;
        }
        if (dipType == DipType.TYPE_TOP) {
            return topHeadValue;
        }
        return 0;
    }

    /**
     * 修改边距
     *
     * @param edgeDistance
     */
    public void setEdgeDistance(int edgeDistance) {
        this.edgeDistance = edgeDistance;
        setHeadDirectionDraw(dipType);
    }

    /**
     * index =0 ,1 ,2 ,3
     * 长度
     *
     * @param index
     * @return
     */
    public int getLengthByIndex(int index, int endIndex) {
        if (mOrangeLatLngs.size() > 0) {
            List<LatLng> latLngs = new ArrayList<>();
            for (int i = index; i <= endIndex; i++) {
                latLngs.add(mOrangeLatLngs.get(i));
            }
            return MapCreateManager.getMapManager().getLatLngsLengh(latLngs);
        } else {
            return 0;
        }

    }

    public LatLng getLatLngByIndex(int index) {
        return mOrangeLatLngs.get(index);
    }

    /**
     * 航线长度
     *
     * @return 米
     */
    public int getLength() {
        return MapCreateManager.getMapManager().getLatLngsLengh(mOrangeLatLngs);
    }

    public int getLength(int startIndex, int endIndex) {
        if (mOrangeLatLngs == null) {
            return 0;
        }
        if (mOrangeLatLngs.size() == 0) {
            return 0;
        }
        if (startIndex < 0) {
            startIndex = 0;
        }
        if (startIndex >= mOrangeLatLngs.size()) {
            startIndex = 0;
        }
        if (endIndex >= mOrangeLatLngs.size()) {
            endIndex = mOrangeLatLngs.size() - 1;
        }
        if (endIndex < 0) {
            endIndex = mOrangeLatLngs.size() - 1;
        }
        //0 -8;
        List<LatLng> latLngs = new ArrayList<>();
        for (int i = startIndex; i <= endIndex; i++) {
            latLngs.add(mOrangeLatLngs.get(i));
        }
        return MapCreateManager.getMapManager().getLatLngsLengh(latLngs);
    }

    /**
     * 航线面积
     *
     * @return 米
     */
    public int getArea() {
        return (int) MapCalculationUtil.work3DOutArea(mDrawLatLngs);
    }

    /**
     * 修改高度
     *
     * @param flyHeight
     */
    public void setFlyHeight(int flyHeight) {
        this.flyHeight = flyHeight;
        setGsd(flyHeight);
        setRouteCourseIntervalDistance(flyHeight, focalLength, shortFormat, courseOverlapRatio);
        setRouteIntervalDistance(flyHeight, focalLength, longFormat, diskOverlapRatio);
    }

    public int getFlyHeight() {
        return flyHeight;
    }

    private void setGsd(int flyHeight) {
        gsd = baseGsd * flyHeight * 0.01;
        DecimalFormat decimalFormat = new DecimalFormat("###0.00");
        gsd = Double.parseDouble(decimalFormat.format(gsd));
    }

    private void postAllData() {

        if (mOnDataChangeListener != null) {
            mOnDataChangeListener.dataChange();
        }
    }

    /**
     * 修改航向重叠率
     *
     * @param courseOverlapRatio
     */
    public void setCourseOverlapRatio(int courseOverlapRatio) {
        this.courseOverlapRatio = courseOverlapRatio;
        setRouteCourseIntervalDistance(flyHeight, focalLength, shortFormat, courseOverlapRatio);
    }

    /**
     * 修改盘向重叠率
     *
     * @param diskOverlapRatio
     */
    public void setDiskOverlapRatio(int diskOverlapRatio) {
        this.diskOverlapRatio = diskOverlapRatio;
        setRouteIntervalDistance(flyHeight, focalLength, longFormat, diskOverlapRatio);
    }

    /**
     * 修改焦距
     *
     * @param focalLength
     */
    public void setFocalLength(double focalLength, double gsd) {
        this.focalLength = focalLength;
        this.baseGsd = gsd;
        setRouteIntervalDistance(flyHeight, focalLength, longFormat, diskOverlapRatio);
        setGsd(flyHeight);
    }


    /**
     * GSD
     *
     * @return
     */
    public double getGsd() {
        return gsd;
    }

    /**
     * 改变高度和航向重叠率
     * 航向间隔
     *
     * @param height           高度
     * @param focalLength      焦距
     * @param shortFormat      短画幅
     * @param diskOverlapRatio 重叠率
     */
    public void setRouteCourseIntervalDistance(double height, double focalLength, double shortFormat, double diskOverlapRatio) {
        double routeCourseTrueDistance = getRouteCourseTrueDistance(height, focalLength, shortFormat);
        courseIntervalDistance = (int) (routeCourseTrueDistance - routeCourseTrueDistance * diskOverlapRatio * 0.01);
        setCourseIntervalTime();
    }


    public void setCourseIntervalTime() {
        if (mDecimalFormat == null) {
            mDecimalFormat = new DecimalFormat("#0.0");
        }
        courseIntervalTime = (courseIntervalDistance * 1.0) / (flySpeed * 1.0);
        courseIntervalTime = Double.parseDouble(mDecimalFormat.format(courseIntervalTime));
    }

    /**
     * 间隔距离
     *
     * @return
     */
    public double getCourseIntervalDistance() {
        return courseIntervalDistance;
    }

    /**
     * 间隔时间
     *
     * @return
     */
    public double getCourseIntervalTime() {
        return courseIntervalTime;
    }


    /**
     * 改变高度和盘向重叠率
     * 盘向间隔
     *
     * @param height           高度
     * @param focalLength      焦距
     * @param longFormat       长画幅
     * @param diskOverlapRatio 重叠率
     */
    public void setRouteIntervalDistance(double height, double focalLength, double longFormat, double diskOverlapRatio) {
        double routeTrueDistance = getRouteTrueDistance(height, focalLength, longFormat);
        routeIntervalDistance = (int) (routeTrueDistance - routeTrueDistance * diskOverlapRatio * 0.01);
        setHeadDirectionDraw(dipType);
    }


    /**
     * 盘向拍摄真实距离
     *
     * @param height      高度
     * @param focalLength 焦距
     * @param longFormat  长画幅
     * @param height
     */
    public double getRouteTrueDistance(double height, double focalLength, double longFormat) {
        focalLength = focalLength / 1000.0;
        longFormat = longFormat / 1000.0;
        return height * longFormat / focalLength;
    }

    /**
     * 航向拍摄真实距离
     *
     * @param height      高度
     * @param focalLength 焦距
     * @param shortFormat 短画幅
     * @param height
     */
    public double getRouteCourseTrueDistance(double height, double focalLength, double shortFormat) {
        focalLength = focalLength / 1000.0;
        shortFormat = shortFormat / 1000.0;
        return height * shortFormat / focalLength;
    }

    /**
     * 纬度差
     * 边距
     * 纬度的距离：一个1纬度是111000m,0.001度即是111m.
     * 经度的距离：111000×0.001×cosA（A为点所在纬度）28.166934427802392
     */
    public double getLngUnits(double lat) {
        //纬度米
        double ax = 1.0 / (111000.0);
        double latDistance = edgeDistance * ax;
        double dd = latDistance * Math.cos(lat);
        return Math.abs(dd);
    }

    /**
     * 缩放比列 算法
     * todo 有待研究算法
     *
     * @return
     */
    public double getScale() {
        if (edgeDistance >= 0 && edgeDistance <= 10) {
            return 1.1;
        }
        if (edgeDistance > 10 && edgeDistance <= 20) {
            return 1.15;
        }
        if (edgeDistance > 20 && edgeDistance <= 30) {
            return 1.2;
        }
        if (edgeDistance > 30 && edgeDistance <= 40) {
            return 1.25;
        }
        if (edgeDistance > 40 && edgeDistance <= 50) {
            return 1.3;
        }
        if (edgeDistance > 50 && edgeDistance <= 60) {
            return 1.35;
        }
        if (edgeDistance > 60 && edgeDistance <= 70) {
            return 1.4;
        }
        if (edgeDistance > 70 && edgeDistance <= 80) {
            return 1.42;
        }
        if (edgeDistance > 80 && edgeDistance <= 90) {
            return 1.46;
        }
        if (edgeDistance > 90 && edgeDistance <= 100) {
            return 1.52;
        }
        return 1.48;
    }

    /**
     * 通过类型更新航线
     */
    private void updateWayLineByType(List<LatLng> orangeLatLngs) {
        List<LatLng> routeLatLngs = new ArrayList<>(orangeLatLngs);
        if (dipType == 1) {
            mRouteLatLngs = routeLatLngs;
            if (mRoutePolyline == null) {
                mRoutePolyline = MapCreateManager.getMapManager().drawBlueLine(routeLatLngs);
            } else {
                mRoutePolyline.setPoints(routeLatLngs);
            }
            if (routeLatLngs != null && routeLatLngs.size() > 0) {
                if (mStartMarker != null) {
                    mStartMarker.setPosition(routeLatLngs.get(0));
                } else {
                    mStartMarker = MapCreateManager.getMapManager().setStartMarkerView(UIUtils.getContext(), routeLatLngs.get(0), "S");
                }
            }
        } else if (dipType == DipType.TYPE_RIGHT) { //右偏
            LogUtil.e("正射", "===========DipType.TYPE_RIGHT=======================>");
            // 90---0 ----270  || 129 ---
            mRightRouteLatLngs = tiltAngleDistance(270 - (mHeadDirection - 90), routeLatLngs);
        } else if (dipType == DipType.TYPE_BOTTOM) {//底部
            LogUtil.e("正射", "===========DipType.TYPE_BOTTOM=======================>");
            //90---90 -----0    ||
            mBottomRouteLatLngs = tiltAngleDistance(-(mHeadDirection - 90), routeLatLngs);
        } else if (dipType == DipType.TYPE_LEFT) {//左偏
            LogUtil.e("正射", "===========DipType.TYPE_LEFT=======================>");
            //90---180  -----90  ||
            mLeftRouteLatLngs = tiltAngleDistance(90 - (mHeadDirection - 90), routeLatLngs);
        } else if (dipType == DipType.TYPE_TOP) { //上偏
            LogUtil.e("正射", "===========DipType.TYPE_TOP=======================>");
            //90-----270----180  ||
            mTopRouteLatLngs = tiltAngleDistance(180 - (mHeadDirection - 90), routeLatLngs);
        }
    }

    public List<LatLng> getCenterRouteLatLngs() {
        return mRouteLatLngs;
    }

    public List<LatLng> getLeftRouteLatLngs() {
        return mLeftRouteLatLngs;
    }

    public List<LatLng> getTopRouteLatLngs() {
        return mTopRouteLatLngs;
    }

    public List<LatLng> getRightRouteLatLngs() {
        return mRightRouteLatLngs;
    }

    public List<LatLng> getBottomRouteLatLngs() {
        return mBottomRouteLatLngs;
    }

    /**
     * 倾斜角度距离
     * <p>
     * azim
     */
    public List<LatLng> tiltAngleDistance(double pitchAngle, List<LatLng> routeLatLngs) {
        List<LatLng> latLngss = new ArrayList<>();
        double tiltDistance = getTiltDistance();
        for (int i = 0; i < routeLatLngs.size(); i++) {
            LatLng latLng = getTiltLatlng(routeLatLngs.get(i), tiltDistance, pitchAngle);
            latLngss.add(latLng);
        }
        if (mRoutePolyline == null) {
            mRoutePolyline = MapCreateManager.getMapManager().drawBlueLine(latLngss);
        } else {
            mRoutePolyline.setPoints(latLngss);
        }
        if (routeLatLngs != null && routeLatLngs.size() > 0) {
            if (mStartMarker != null) {
                mStartMarker.setPosition(routeLatLngs.get(0));
            } else {
                mStartMarker = MapCreateManager.getMapManager().setStartMarkerView(UIUtils.getContext(), routeLatLngs.get(0), "S");
            }
        }
        return latLngss;
    }


    /**
     * 增加区域点带加号的点
     *
     * @param context
     * @param beforeLatLng
     * @param latterLatLng
     * @param position
     * @return
     */
    public Marker addMiddleMarker(Context context, LatLng beforeLatLng, LatLng latterLatLng, int position, boolean isDrag) {
        double distance = MapCalculationLogLatDistance.getDistanceTwoMi(beforeLatLng.longitude, beforeLatLng.latitude, latterLatLng.longitude, latterLatLng.latitude);
        LatLng middleLatLng = MapCalculationLogLatDistance.getMiddleLatLng(beforeLatLng, latterLatLng);
        return getMiddlePlusSignMarkerView(context, middleLatLng, position, distance, isDrag);
    }


    /**
     * 增加区域点
     *
     * @param context
     * @param latLng
     * @param position
     * @return
     */
    public Marker addMarker(Context context, LatLng latLng, int position, boolean isDrag) {
        return getPointMarkerView(context, latLng, position, isDrag);
    }

    /**
     * 增加带加号航点的Marker按钮
     * 带加号的
     *
     * @param context
     * @param latLng
     * @param position
     * @param distance
     * @return
     */
    public Marker getMiddlePlusSignMarkerView(Context context, LatLng latLng, int position, double distance, boolean isDrag) {
        DroneMiddlePointMarkerBinding middlePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_middle_point_marker, null, false);
        DecimalFormat decimalFormat = new DecimalFormat("###0.0");
        middlePointMarkerBinding.tvMiddlePoint.setText(decimalFormat.format(distance) + "m");
        MarkerOptions markerOptions = new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromView(middlePointMarkerBinding.getRoot())).position(latLng)
                .draggable(isDrag).anchor(0.5f, 0.7f);//第几个点 从1开始
        Marker marker = MapCreateManager.getMapManager().getaMap().addMarker(markerOptions);
        marker.setSnippet(0 + "");
        marker.setZIndex(10);
        marker.setPeriod(position);
        marker.setObject(MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD_MIDDLE);
        return marker;
    }

    /**
     * 最后一个点 未选中
     *
     * @param context
     * @param marker
     * @param position
     */
    public void setMiddleMarker(Context context, Marker marker, int position, double distance) {
        DroneMiddlePointMarkerBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_middle_point_marker, null, false);
        DecimalFormat decimalFormat = new DecimalFormat("###0.0");
        binding.tvMiddlePoint.setText(decimalFormat.format(distance) + "m");
        marker.setIcon(BitmapDescriptorFactory.fromView(binding.getRoot()));
        marker.setPeriod(position);
        marker.setZIndex(10);
        marker.setObject(MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD_MIDDLE);
    }


    public Marker getPointMarkerView(Context context, LatLng latLng, int position, boolean isDrag) {
        DroneRegionPointMarkerBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_region_point_marker, null, false);
        binding.tvName.setText(position + "");
        MarkerOptions markerOptions = new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromView(binding.getRoot())).position(latLng)
                .draggable(isDrag).anchor(0.5f, 0.5f);
        Marker marker = MapCreateManager.getMapManager().getaMap().addMarker(markerOptions);
        marker.setZIndex(10);
        marker.setPeriod(position);

        marker.setObject(MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD);
        return marker;
    }


    /**
     * 最后一个点 未选中
     *
     * @param context
     * @param marker
     * @param position
     */
    public void addMiddleMarker(Context context, Marker marker, int position) {
        DroneRegionPointMarkerBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_region_point_marker, null, false);
        binding.tvName.setText(position + "");
        marker.setIcon(BitmapDescriptorFactory.fromView(binding.getRoot()));
        marker.setPeriod(position);
        marker.setZIndex(10);
        marker.setObject(MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD);
    }

    /**
     * 清除
     */
    public void clearMap() {
        mLatLng1 = null;
        mLatLng2 = null;
        mDragLatLng = null;
        dragStartLatLng = null;
        dipType = DipType.TYPE_CENTER;
        if (mBoundShadowDrawLatLngs != null) {
            mBoundShadowDrawLatLngs.clear();
        }
        if (mRoutePolyline != null) {
            mRoutePolyline.remove();
            mRoutePolyline = null;
        }
        if (mRouteLatLngs != null) {
            mRouteLatLngs.clear();
        }
        if (mOrangeLatLngs != null) {
            mOrangeLatLngs.clear();
        }
        if (mDrawPolygonOptions != null) {
            mDrawPolygonOptions.remove();
            mDrawPolygonOptions = null;
        }

        if (mCurrentEdgMarker != null) {
            mCurrentEdgMarker.remove();
            mCurrentEdgMarker = null;
        }
        if (mStartMarker != null) {
            mStartMarker.remove();
            mStartMarker = null;
        }
        if (mDrawLatLngs != null) {
            mDrawLatLngs.clear();
        }
        if (mDrawMarkers != null) {
            for (int i = 0; i < mDrawMarkers.size(); i++) {
                mDrawMarkers.get(i).remove();
            }
            mDrawMarkers.clear();
        }
        if (mDrawPlusMarkers != null) {
            for (int i = 0; i < mDrawPlusMarkers.size(); i++) {
                mDrawPlusMarkers.get(i).remove();
            }
            mDrawPlusMarkers.clear();
        }
        if (mDrawPolylines != null) {
            for (int i = 0; i < mDrawPolylines.size(); i++) {
                mDrawPolylines.get(i).remove();
            }
            mDrawPolylines.clear();
        }
        if (mDrawPlusLatLng != null) {
            mDrawPlusLatLng.clear();
        }
    }

    /**
     * 撤销一个区域点 >=3
     */
    public void revokeWayPointEdgLine(MarkerCallback markerCallback) {
        if (mDrawLatLngs.size() > 3) {
            if (mCurrentEdgMarker != null) {
                int type = (int) mCurrentEdgMarker.getObject();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD) {
                    int clickPosition = mCurrentEdgMarker.getPeriod();
                    revokeEdg(clickPosition);
                }
            } else {
                int clickPosition = mDrawMarkers.size() - 1;
                revokeEdg(clickPosition);
            }
        }
        mCurrentEdgMarker = mDrawMarkers.get(mDrawMarkers.size() - 1);
        //数量
        if (markerCallback != null) {
            markerCallback.onCallBack(mDrawLatLngs.size());
        }
    }


    private void revokeEdg(int clickPosition) {
        //区域点
        mDrawLatLngs.remove(clickPosition - 1);
        if (mDrawMarkers != null) {
            for (int i = 0; i < mDrawMarkers.size(); i++) {
                mDrawMarkers.get(i).remove();
            }
            mDrawMarkers.clear();
        }
        if (mDrawPlusMarkers != null) {
            for (int i = 0; i < mDrawPlusMarkers.size(); i++) {
                mDrawPlusMarkers.get(i).remove();
            }
            mDrawPlusMarkers.clear();
        }
        if (mDrawPolylines != null) {
            for (int i = 0; i < mDrawPolylines.size(); i++) {
                mDrawPolylines.get(i).remove();
            }
            mDrawPolylines.clear();
        }
        if (mDrawPlusLatLng != null) {
            mDrawPlusLatLng.clear();
        }
        drawEdgLineMarker(UIUtils.getContext());
    }

    /**
     * 开始拖动
     *
     * @param marker
     */
    public void onMarkerDragStart(Marker marker) {
        if (marker.getObject() != null) {
            try {
                isIntersectDrag = false;
                //marker点的类型 常量类 MapMarkerTypeConstant
                int type = (int) marker.getObject();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD) {
                    //航点 业务逻辑处理
                    dragWayPointMarkerStart(marker);
                }
                if (type == MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD_MIDDLE) {
                    //增加点
                    clickPosition = marker.getPeriod();
                    dragStartLatLng = mDrawPlusLatLng.get(clickPosition - 1);
                    marker.setPosition(dragStartLatLng);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 拖动中
     *
     * @param marker
     */
    public void onMarkerDrag(Marker marker) {
        if (marker.getObject() != null) {
            try {
                //marker点的类型 常量类 MapMarkerTypeConstant
                int type = (int) marker.getObject();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD) {
                    //航点 业务逻辑处理
                    mCurrentEdgMarker = marker;
                    int clickPosition = marker.getPeriod();
                    dragWayPointMarker(clickPosition, marker);
                    //todo 判断和边缘线是否有交点
                    hasIntersectAndEdgLine(clickPosition);
                    if (isIntersectDrag) {
                        //红线条
                        for (int i = 0; i < mDrawPolylines.size(); i++) {
                            mDrawPolylines.get(i).setColor(0xffFF0000);
                        }
                    } else {
                        //蓝色线条
                        for (int i = 0; i < mDrawPolylines.size(); i++) {
                            mDrawPolylines.get(i).setColor(0xff00FF55);
                        }
                    }
                }
                if (type == MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD_MIDDLE) {
                    //增加点
                    mDragLatLng = marker.getPosition();
                    int clickPosition = marker.getPeriod();
                    dragPlusMarker(mDragLatLng, clickPosition);
                    hasIntersectAndPlusLine(clickPosition);
                    if (isIntersectDrag) {
                        //红线条
                        for (int i = 0; i < mDrawPolylines.size(); i++) {
                            mDrawPolylines.get(i).setColor(0xffFF0000);
                        }
                    } else {
                        //蓝色线条
                        for (int i = 0; i < mDrawPolylines.size(); i++) {
                            mDrawPolylines.get(i).setColor(0xff00FF55);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 结束拖动
     *
     * @param marker
     */
    public void onMarkerDragEnd(Marker marker) {
        if (marker.getObject() != null) {
            try {
                //marker点的类型 常量类 MapMarkerTypeConstant
                int type = (int) marker.getObject();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD) { //航点 业务逻辑处理
                    mCurrentEdgMarker = marker;
                    if (!isIntersectDrag) {
                        int clickPosition = marker.getPeriod();
                        dragWayPointMarker(clickPosition, marker);
                        setHeadDirectionDraw(dipType);
                    } else {
                        restoreWayPointMarker(marker);
                    }
                }
                if (type == MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD_MIDDLE) {
                    if (!isIntersectDrag) {
                        int clickPosition = marker.getPeriod();
                        //增加点
                        addEdgMarkerPoint(clickPosition, marker);
                    } else {
                        restoreWayPlusPointMarker(marker);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        mLatLng1 = null;
        mLatLng2 = null;
        mDragLatLng = null;
    }

    /**
     * 拖动带加号的区域点
     *
     * @param gragLatLng
     * @param clickPosition
     */
    private void dragPlusMarker(LatLng gragLatLng, int clickPosition) {
        //改变连线
        int position1;
        int position2;
        if (clickPosition == mDrawLatLngs.size()) {
            position1 = clickPosition - 1;
            position2 = 0;
        } else {
            position1 = clickPosition - 1;
            position2 = clickPosition;
        }
        mLatLng1 = mDrawLatLngs.get(position1);
        mLatLng2 = mDrawLatLngs.get(position2);
        List<LatLng> latLngs = new ArrayList<>();
        latLngs.add(mLatLng1);
        latLngs.add(gragLatLng);
        latLngs.add(mLatLng2);
        Polyline polyline = mDrawPolylines.get(clickPosition - 1);
        polyline.setPoints(latLngs);
    }

    /**
     * 判断是否有交点
     *
     * @param clickPosition
     */
    private void hasIntersectAndEdgLine(int clickPosition) {
        for (int i = 0; i < mDrawLatLngs.size(); i++) {
            if (clickPosition == 1) {
                if (i != clickPosition - 1 && i != mDrawLatLngs.size() - 1) {
                    LatLng latLng1 = mDrawLatLngs.get(i);
                    LatLng latLng2 = mDrawLatLngs.get(i + 1);
                    LogUtil.e("航线", "=============>" + i);
                    boolean b1 = MapCalculationLogLatDistance.intersect(mLatLng1, mDragLatLng, latLng1, latLng2);
                    boolean b2 = MapCalculationLogLatDistance.intersect(mDragLatLng, mLatLng2, latLng1, latLng2);
                    if (b1 || b2) {
                        isIntersectDrag = true;
                        break;
                    } else {
                        isIntersectDrag = false;
                    }
                }
            } else if (clickPosition == mDrawLatLngs.size()) {
                if (i != mDrawLatLngs.size() - 2 && i != mDrawLatLngs.size() - 1) {
                    LogUtil.e("航线", "=============>" + i);
                    LatLng latLng1 = mDrawLatLngs.get(i);
                    LatLng latLng2 = mDrawLatLngs.get(i + 1);
                    boolean b1 = MapCalculationLogLatDistance.intersect(mLatLng1, mDragLatLng, latLng1, latLng2);
                    boolean b2 = MapCalculationLogLatDistance.intersect(mDragLatLng, mLatLng2, latLng1, latLng2);
                    if (b1 || b2) {
                        isIntersectDrag = true;
                        break;
                    } else {
                        isIntersectDrag = false;
                    }
                }
            } else {
                if (i != clickPosition - 2 && i != clickPosition - 1) {
                    LatLng latLng1;
                    LatLng latLng2;
                    if (i == mDrawLatLngs.size() - 1) {
                        latLng1 = mDrawLatLngs.get(i);
                        latLng2 = mDrawLatLngs.get(0);
                    } else {
                        latLng1 = mDrawLatLngs.get(i);
                        latLng2 = mDrawLatLngs.get(i + 1);
                    }
                    boolean b1 = MapCalculationLogLatDistance.intersect(mLatLng1, mDragLatLng, latLng1, latLng2);
                    boolean b2 = MapCalculationLogLatDistance.intersect(mDragLatLng, mLatLng2, latLng1, latLng2);
                    if (b1 || b2) {
                        isIntersectDrag = true;
                        break;
                    } else {
                        isIntersectDrag = false;
                    }
                }
            }
        }
    }

    /**
     * 判断是否有交点
     *
     * @param clickPosition
     */
    private void hasIntersectAndPlusLine(int clickPosition) {
        for (int i = 0; i < mDrawLatLngs.size(); i++) {
            if (i != clickPosition - 1) {
                LatLng latLng1;
                LatLng latLng2;
                if (i == mDrawLatLngs.size() - 1) {
                    latLng1 = mDrawLatLngs.get(i);
                    latLng2 = mDrawLatLngs.get(0);
                } else {
                    latLng1 = mDrawLatLngs.get(i);
                    latLng2 = mDrawLatLngs.get(i + 1);
                }
                boolean b1 = MapCalculationLogLatDistance.intersect(mLatLng1, mDragLatLng, latLng1, latLng2);
                boolean b2 = MapCalculationLogLatDistance.intersect(mDragLatLng, mLatLng2, latLng1, latLng2);
                if (b1 || b2) {
                    isIntersectDrag = true;
                    break;
                } else {
                    isIntersectDrag = false;
                }
            }

        }
    }


    /**
     * 开始拖动
     *
     * @param marker
     */
    public void dragWayPointMarkerStart(Marker marker) {
        mCurrentEdgMarker = marker;
        clickPosition = marker.getPeriod();
        dragStartLatLng = mDrawLatLngs.get(clickPosition - 1);
        marker.setPosition(dragStartLatLng);

    }

    /**
     * 还原区域线
     */
    public void restoreWayPointMarker(Marker marker) {
        if (isIntersectDrag) {//
            isIntersectDrag = false;
            marker.setPosition(dragStartLatLng);
            mDrawLatLngs.set(clickPosition - 1, dragStartLatLng);
            dragRouteWayPointMarker(clickPosition, dragStartLatLng);
            for (int i = 0; i < mDrawPolylines.size(); i++) {
                mDrawPolylines.get(i).setColor(0xff00FF55);
            }
        }
    }


    /**
     * 还原带加号的区域线
     */
    public void restoreWayPlusPointMarker(Marker marker) {
        if (isIntersectDrag) {//
            isIntersectDrag = false;
            marker.setPosition(dragStartLatLng);
            mDrawPlusLatLng.set(clickPosition - 1, dragStartLatLng);
            dragPlusMarker(dragStartLatLng, clickPosition);
            for (int i = 0; i < mDrawPolylines.size(); i++) {
                mDrawPolylines.get(i).setColor(0xff00FF55);
            }
        }
    }

    /**
     * 拖动中
     *
     * @param clickPosition
     * @param marker
     */
    public void dragWayPointMarker(int clickPosition, Marker marker) {
        mDragLatLng = marker.getPosition();
        dragRouteWayPointMarker(clickPosition, mDragLatLng);

    }

    private void dragRouteWayPointMarker(int clickPosition, LatLng dragLatLng) {
        mDrawLatLngs.set(clickPosition - 1, dragLatLng);
        LogUtil.e("航线", "================>" + clickPosition + "===p1:");
        //线段改变
        int position1;
        int position2;
        if (clickPosition == mDrawLatLngs.size()) {//4
            position1 = clickPosition - 2;//2
            position2 = mDrawLatLngs.size() - 1; //0
            mLatLng1 = mDrawLatLngs.get(clickPosition - 2);
            mLatLng2 = mDrawLatLngs.get(0);
        } else if (clickPosition == 1) {   // 1
            position1 = mDrawLatLngs.size() - 1;
            position2 = clickPosition - 1;

            mLatLng1 = mDrawLatLngs.get(mDrawLatLngs.size() - 1);
            mLatLng2 = mDrawLatLngs.get(clickPosition);
        } else { //2
            position1 = clickPosition - 2;//1
            position2 = clickPosition - 1;//2
            mLatLng1 = mDrawLatLngs.get(clickPosition - 2);
            mLatLng2 = mDrawLatLngs.get(clickPosition);
        }
        //连线1
        Polyline polyline1 = mDrawPolylines.get(position1);
        List<LatLng> latLngs1 = new ArrayList<>();
        latLngs1.add(mLatLng1);
        latLngs1.add(dragLatLng);
        polyline1.setPoints(latLngs1);
        //连线2
        Polyline polyline2 = mDrawPolylines.get(position2);
        List<LatLng> latLngs2 = new ArrayList<>();
        latLngs2.add(dragLatLng);
        latLngs2.add(mLatLng2);
        polyline2.setPoints(latLngs2);

        //加号1
        Marker marker1 = mDrawPlusMarkers.get(position1);
        double distance1 = MapCalculationLogLatDistance.getDistanceTwoMi(mLatLng1.longitude, mLatLng1.latitude, dragLatLng.longitude, dragLatLng.latitude);
        LatLng middleLatLng1 = MapCalculationLogLatDistance.getMiddleLatLng(mLatLng1, dragLatLng);
        marker1.setPosition(middleLatLng1);
        mDrawPlusLatLng.set(position1, middleLatLng1);
        ObliquePhotographyUtil.getInstance().setMiddleMarker(UIUtils.getContext(), marker1, position1 + 1, distance1);
        //加号2
        Marker marker2 = mDrawPlusMarkers.get(position2);
        double distance2 = MapCalculationLogLatDistance.getDistanceTwoMi(dragLatLng.longitude, dragLatLng.latitude, mLatLng2.longitude, mLatLng2.latitude);
        LatLng middleLatLng2 = MapCalculationLogLatDistance.getMiddleLatLng(dragLatLng, mLatLng2);
        marker2.setPosition(middleLatLng2);
        mDrawPlusLatLng.set(position2, middleLatLng2);
        ObliquePhotographyUtil.getInstance().setMiddleMarker(UIUtils.getContext(), marker2, position2 + 1, distance2);
    }


    /**
     * 点击
     *
     * @param marker
     */
    public void onMarkerClick(Marker marker) {
        if (marker.getObject() != null) {
            try {
                //marker点的类型 常量类 MapMarkerTypeConstant
                int type = (int) marker.getObject();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD) { //航点 业务逻辑处理
                }
                if (type == MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD_MIDDLE) { //增加点
                    int clickPosition = marker.getPeriod();
                    addEdgMarkerPoint(clickPosition, marker);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 增加区域点
     *
     * @param clickPosition
     * @param marker
     */
    private void addEdgMarkerPoint(int clickPosition, Marker marker) {
        LogUtil.e("航线+", "================>" + clickPosition);
        int position1;
        int position2;
        if (clickPosition == mDrawLatLngs.size()) {
            position1 = clickPosition - 1;
            position2 = 0;
        } else {
            position1 = clickPosition - 1;
            position2 = clickPosition;
        }

        Polyline polyline = mDrawPolylines.get(clickPosition - 1);

        //改变连线
        LatLng latLng1 = mDrawLatLngs.get(position1);
        LatLng dragLatLng = marker.getPosition();
        LatLng latLng2 = mDrawLatLngs.get(position2);
        //
        List<LatLng> latLngs = new ArrayList<>();
        latLngs.add(latLng1);
        latLngs.add(dragLatLng);
        polyline.setPoints(latLngs);
        Polyline polyline2 = MapCreateManager.getMapManager().drawTwoPointBlueLine(dragLatLng, latLng2);
        if (clickPosition == mDrawLatLngs.size()) {
            mDrawPolylines.add(polyline2);
        } else {
            mDrawPolylines.add(position2, polyline2);
        }
        //增加点
        marker.destroy();
        mDrawPlusMarkers.remove(clickPosition - 1);
        mDrawPlusLatLng.remove(clickPosition - 1);
        Marker plusMarker1 = ObliquePhotographyUtil.getInstance().addMiddleMarker(UIUtils.getContext(), latLng1, dragLatLng, clickPosition, true);
        Marker plusMarker2 = ObliquePhotographyUtil.getInstance().addMiddleMarker(UIUtils.getContext(), dragLatLng, latLng2, clickPosition + 1, true);
        mDrawPlusMarkers.add(clickPosition - 1, plusMarker1);
        mDrawPlusMarkers.add(clickPosition, plusMarker2);
        mDrawPlusLatLng.add(clickPosition - 1, plusMarker1.getPosition());
        mDrawPlusLatLng.add(clickPosition, plusMarker2.getPosition());
        //区域点
        mDrawLatLngs.add(clickPosition, dragLatLng);
        Marker edgMarker = ObliquePhotographyUtil.getInstance().addMarker(UIUtils.getContext(), dragLatLng, clickPosition + 1, true);
        mDrawMarkers.add(clickPosition, edgMarker);
        mCurrentEdgMarker = edgMarker;
        sortWayPointMarkerByPosition(UIUtils.getContext(), clickPosition, mDrawMarkers);
        sortAddSignWayPointMarkerByClickPosition(clickPosition, mDrawPlusMarkers);
        setHeadDirectionDraw(dipType);
    }


    /**
     * 区域点排序
     */
    public void sortWayPointMarkerByPosition(Context context, int clickPosition, List<Marker> markerLatLng) {
        int statPosition = clickPosition + 1;//2
        for (int i = statPosition; i < markerLatLng.size(); i++) {
            Marker marker = markerLatLng.get(i);// 2
            if (marker.getObject() != null) {
                int type = (int) marker.getObject();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD) { //航点 业务逻辑处理
                    marker.setPeriod(i + 1);
                    addMiddleMarker(context, marker, i + 1);
                }
            }
        }
    }


    /**
     * 带加号的航点排序
     *
     * @param clickPosition
     * @param markerLatLng
     */
    public void sortAddSignWayPointMarkerByClickPosition(int clickPosition, List<Marker> markerLatLng) {
        int startPosition = clickPosition - 1;
        for (int i = startPosition; i < markerLatLng.size(); i++) {
            Marker marker = markerLatLng.get(i);// 2
            if (marker.getObject() != null) {
                int type = (int) marker.getObject();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_POLYGON_ADD_MIDDLE) { //航点 业务逻辑处理
                    marker.setPeriod(i + 1);
                }
            }
        }
    }


    public interface MarkerCallback {
        void onCallBack(int size);
    }

    /**
     * 获取航点数量
     *
     * @return 航线点的数量
     */
    public List<LatLng> getRouteLatLngs() {
        if (dipType == DipType.TYPE_CENTER) { //中
            return getCenterRouteLatLngs();
        }
        if (dipType == DipType.TYPE_BOTTOM) {
            return getBottomRouteLatLngs();
        }
        if (dipType == DipType.TYPE_RIGHT) {
            return getRightRouteLatLngs();
        }
        if (dipType == DipType.TYPE_TOP) {
            return getTopRouteLatLngs();
        }
        if (dipType == DipType.TYPE_LEFT) {
            return getLeftRouteLatLngs();
        }
        return mRouteLatLngs;
    }


    /**
     * 获取面积
     *
     * @return
     */
    public double getRegionArea() {
        return MapCalculationUtil.workOutArea(mDrawLatLngs);
    }

    public int getLineNumbers() {
        return getRouteLatLngs().size();

    }

    public int getWayPointPictures(double distance) {
        String timeStr = "0";
        int time = 0;
        if (flySpeed > 0) {
            time = (int) (distance / flySpeed);
        } else {
            flySpeed = 6;
            time = (int) (distance / flySpeed);
        }
        double courseIntervalTime = (courseIntervalDistance * 1.0) / flySpeed;
        int pictures = (int) (time / courseIntervalTime);
        return pictures + getLineNumbers();
    }

    /**
     * 飞行时间
     *
     * @param distance
     * @return 时分秒
     */
    public String getWayPointFlyTime(double distance) {
        String timeStr = "0";
        int time = 0;
        if (flySpeed > 0) {
            time = (int) (distance / flySpeed);
        }
        timeStr = DateUtils.getHMSByMillisecondTimeStamp(time * 1000);
        return timeStr;
    }

    public String getWayPointFlyTime(double distance, int sIndex, int endIndex) {
        String timeStr = "0";
        int time = 0;
        if (flySpeed > 0) {
            time = (int) (distance / flySpeed);
        }
        int sindex = endIndex - sIndex + 1;
        time = time + sindex * 3;
        timeStr = DateUtils.getHMSByMillisecondTimeStamp(time * 1000);
        return timeStr;
    }

    public int getFlySpeed() {
        return flySpeed;
    }

    public void setFlySpeed(int flySpeed) {
        this.flySpeed = flySpeed;
        setCourseIntervalTime();
        postAllData();
    }

    /**
     * 隐藏和显示航带点
     *
     * @param visible
     */
    public void setMarkerVisible(boolean visible) {
        if (mDrawMarkers != null && mDrawMarkers.size() > 0) {
            for (int i = 0; i < mDrawMarkers.size(); i++) {
                mDrawMarkers.get(i).setVisible(visible);
            }
        }
    }

    /**
     * 隐藏和显示加号
     *
     * @param visible
     */
    public void setPlusMarkerVisible(boolean visible) {
        if (mDrawPlusMarkers != null && mDrawPlusMarkers.size() > 0) {
            for (int i = 0; i < mDrawPlusMarkers.size(); i++) {
                mDrawPlusMarkers.get(i).setVisible(visible);
            }
        }
    }

    /**
     * 所有航点不能拖动
     */
    public void setMarkerDisEnableDrag() {
        if (mDrawPlusMarkers != null && mDrawPlusMarkers.size() > 0) {
            for (Marker m : mDrawPlusMarkers) {
                m.setDraggable(false);
            }
        }
        if (mDrawMarkers != null && mDrawMarkers.size() > 0) {
            for (Marker m : mDrawMarkers) {
                m.setDraggable(false);
            }
        }

    }

    /**
     * 所有航点能拖动
     */
    public void setMarkerEnableDrag() {
        if (mDrawPlusMarkers != null && mDrawPlusMarkers.size() > 0) {
            for (Marker m : mDrawPlusMarkers) {
                m.setDraggable(true);
            }
        }
        if (mDrawMarkers != null && mDrawMarkers.size() > 0) {
            for (Marker m : mDrawMarkers) {
                m.setDraggable(true);
            }
        }

    }
}
